package de.tabreaderandroid.tabreader.model;

import java.util.LinkedList;
import java.util.List;

import de.tabreaderandroid.tabreader.utils.InvalidTabFormatException;

/**
 * A class describing a line in the origin document that contains meta
 * information (e.g. headings like "Intro" or tuning).
 * 
 * @author André Stannek
 * 
 */
public class MetaLine extends TabFileElement {

	private String line;

	/**
	 * Creates an new {@link ChordedLine} with an origin index.
	 * 
	 * @param originIndex
	 *            the index of the first line of this chorded line in the origin
	 *            document
	 * @param line
	 *            the line text
	 * @see TabFileElement#TabFileElement(int)
	 */
	public MetaLine(final int originIndex, final String line) {
		super(originIndex);
		this.line = line;
	}

	@Override
	public String toString() {
		return line;
	}

	@Override
	public List<String> getLines(int maxCharWidth)
			throws InvalidTabFormatException {
		List<String> result = new LinkedList<String>();
		String[] split = line.split("\\s");
		int i = 0;
		while (i < split.length) {
			StringBuilder line = new StringBuilder();
			if (split[i].length() > maxCharWidth) {
				throw new InvalidTabFormatException(
						"A line in the tab was to long to display!");
			}
			while (i < split.length
					&& line.length() + split[i].length() + 1 < maxCharWidth) {
				line.append(split[i]).append(" ");
				i++;
			}
			result.add(line.toString());
		}
		return result;
	}
}
